//
//  NsDate+Extension.swift
//  ZHLEE_Blog_Swift_Pro
//
//  Created by ZHLEEBlander on 15/10/22.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import Foundation

extension NSDate {

    /**
    判断是否是今年
    
    - parameter target: <#target description#>
    
    - returns: <#return value description#>
    */
    func isThisYear(target: NSDate) -> Bool{
        
        let formatter = NSDateFormatter()
        
        //制定格式化的字符串格式
        formatter.dateFormat = "yyyy"
        
        let targetStr = formatter.stringFromDate(target)
        
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
        
    }
    /**
    判断是否是今天
    
    - parameter target: <#target description#>
    
    - returns: <#return value description#>
    */
    func isToday(target: NSDate) -> Bool{
        
        let formatter = NSDateFormatter()
        
        formatter.dateFormat = "yyyy-MM-dd"
        
        //格式化字符串
        let targetStr = formatter.stringFromDate(target)
        
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
        
    }
    
    func isYesToday(var target: NSDate) -> Bool {
        
        var currentDate = NSDate()
        
        let formatter = NSDateFormatter()
        
        formatter.dateFormat = "yyyy-MM-dd"
        
        //把date转化成string.."yyyy-MM-dd"这种格式
        let targetStr = formatter.stringFromDate(target)
        
        let currentStr = formatter.stringFromDate(currentDate)
        
        //再把string转化成date
        target = formatter.dateFromString(targetStr)!
        currentDate = formatter.dateFromString(currentStr)!
        
        let result = currentDate.timeIntervalSinceDate(target)
        
        if result == 24 * 60 * 60 {
            
            return true
        }
        
        //        printLog(result)
        
        return false
        
    }
    
}